﻿using BLL;
using Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Admin
{
    public partial class config : Form
    {
        List<Sys_config> list_Added = new List<Sys_config>();
        BindingList<Sys_config> bconfigs;
        List<Sys_config> configs;
        DateTimePicker dtp = new DateTimePicker();

        public config()
        {
            InitializeComponent();
            dataGridView1.Controls.Add(dtp);   
            dtp.Visible = false;   
            dtp.Format = DateTimePickerFormat.Custom;  
            dtp.CustomFormat = "yyyy-MM-dd HH:mm:ss";
            dtp.TextChanged += new EventHandler(dtp_TextChange);
        }

        private void dtp_TextChange(object sender, EventArgs e)
        {
            dataGridView1.CurrentCell.Value = dtp.Text.ToString();   //时间控件选择时间时，就把时间赋给所在的单元格
            dtp.Visible = false;
        }
        private void config_Load(object sender, EventArgs e)
        {
            refresh();
        }

        //private void button1_Click(object sender, EventArgs e)
        //{
        //    BLL.B_config b_config = new BLL.B_config();
        //    if (b_config.save_List(configs))
        //        MessageBox.Show("保存成功!");
        //}

        //private void Add_Click(object sender, EventArgs e)
        //{
        //    BLL.B_config b_config = new BLL.B_config();
        //    if (b_config.add_config(list_Added))
        //        MessageBox.Show("添加成功！");
        //}

        private void dataGridView1_UserAddedRow(object sender, DataGridViewRowEventArgs e)
        {
            list_Added.Add(dataGridView1.CurrentRow.DataBoundItem  as Sys_config);
        }

        //private void Del_Click(object sender, EventArgs e)
        //{
        //    List<Sys_config> conf_deleted = new List<Sys_config>();
        //    foreach (DataGridViewRow row in dataGridView1.SelectedRows)
        //    {
        //        conf_deleted.Add(row.DataBoundItem as Sys_config);
        //    }

        //    BLL.B_config b_config = new B_config();

        //    if (b_config.Del(conf_deleted))
        //        MessageBox.Show("删除成功！");

        //    refresh();
        //}

        private void refresh()
        {
            BLL.B_config b_config = new BLL.B_config();
            configs = b_config.list_config();
            bconfigs = new BindingList<Sys_config>(configs);
            dataGridView1.DataSource = bconfigs;
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == -1 || !dataGridView1.Columns[e.ColumnIndex].Name.EndsWith("deadline"))
                return;

            Rectangle _Rectangle = dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true); 
            dtp.Size = new Size(_Rectangle.Width, _Rectangle.Height); 
            dtp.Location = new Point(_Rectangle.X, _Rectangle.Y); 
            dtp.Visible = true;   
        }

        private void dataGridView1_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
        {
            dtp.Visible = false;
        }

        private void uiadd_Click(object sender, EventArgs e)
        {
            BLL.B_config b_config = new BLL.B_config();
            if (b_config.add_config(list_Added))
                MessageBox.Show("添加成功！");
        }

        private void uidelete_Click(object sender, EventArgs e)
        {
            List<Sys_config> conf_deleted = new List<Sys_config>();
            foreach (DataGridViewRow row in dataGridView1.SelectedRows)
            {
                conf_deleted.Add(row.DataBoundItem as Sys_config);
            }

            BLL.B_config b_config = new B_config();

            if (b_config.Del(conf_deleted))
                MessageBox.Show("删除成功！");

            refresh();
        }



        private void uiButtonsave1_Click(object sender, EventArgs e)
        {
            BLL.B_config b_config = new BLL.B_config();
            if (b_config.save_List(configs))
                MessageBox.Show("保存成功!");
        }
    }
}
